package modeles;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Stock {

	int stock_id;
	String stock_code;
	String stock_name;
	String stock_date_creat;
	
	public Stock (){
		
	}

	
	public Stock(int stock_id, String stock_code, String stock_name) {
		super();
		this.stock_id = stock_id;
		this.stock_code = stock_code;
		this.stock_name = stock_name;
	}


	public int getStock_id() {
		return stock_id;
	}

	public void setStock_id(int stock_id) {
		this.stock_id = stock_id;
	}

	public String getStock_code() {
		return stock_code;
	}

	public void setStock_code(String stock_code) {
		this.stock_code = stock_code;
	}

	public String getStock_name() {
		return stock_name;
	}

	public void setStock_name(String stock_name) {
		this.stock_name = stock_name;
	}

	public String getStock_date_creat() {
		return stock_date_creat;
	}

	public void setStock_date_creat(String stock_date_creat) {
		this.stock_date_creat = stock_date_creat;
	}

	 public Vector<Stock> getListStock(DBManager _dbManager){
	    	Vector<Stock> vectRes = new Vector<Stock>();
	    	String zQueryStock = "SELECT * FROM \"STOCK\" order by stock_nom ASC";

			try
			{
				Statement oStmtStock = (Statement) _dbManager.oStockDbCnx.createStatement () ;	
				ResultSet oRsStock = (ResultSet) oStmtStock.executeQuery (zQueryStock) ;
				while (oRsStock.next ())
				{
					Stock oStock = new Stock();
					
					oStock.setStock_id(oRsStock.getInt("stock_id"));
					oStock.setStock_code(oRsStock.getString("stock_code"));
					oStock.setStock_name(oRsStock.getString("stock_nom"));
					oStock.setStock_date_creat(oRsStock.getString("stock_date_creat"));
					vectRes.add(oStock);
					
				}
			}
			catch (SQLException _e)
			{
				System.out.println ("Erreur interne pendant la requete !") ;
				_e.printStackTrace () ;
			}
	    	return vectRes;
	    }
	
	 public Vector<InfoStock> getListProdByStock(DBManager _dbManager){
		 Vector<InfoStock> vectRes = new Vector<InfoStock>();
		 String zQueryProduit= "SELECT \"INFO_STOCK\".produit_id as id_prod,stock_id,qte_stock,produit_code,produit_libelle,categ_id" +
		 		" FROM \"INFO_STOCK\",\"PRODUIT\" " +
		 		"where \"INFO_STOCK\".produit_id = \"PRODUIT\".produit_id and stock_id = "+this.getStock_id()+"" +
		 				" order by produit_libelle ASC";

			try
			{
				Statement oStmtProduit = (Statement) _dbManager.oStockDbCnx.createStatement () ;	
				ResultSet oRsStock = (ResultSet) oStmtProduit.executeQuery (zQueryProduit) ;
				while (oRsStock.next ())
				{
					InfoStock oInfoStock = new InfoStock ();
					oInfoStock.setProduit_id(oRsStock.getInt("id_prod"));
					oInfoStock.setStock_id(oRsStock.getInt("stock_id"));
					oInfoStock.setQte(oRsStock.getInt("qte_stock"));
					oInfoStock.setInfoStock_produit_code(oRsStock.getString("produit_code"));
					oInfoStock.setInfoStock_produit_libelle(oRsStock.getString("produit_libelle"));
					
					
					vectRes.add(oInfoStock);
					
				}
			}
			catch (SQLException _e)
			{
				System.out.println ("Erreur interne pendant la requete !") ;
				_e.printStackTrace () ;
			}
		 
		 return vectRes ;
	 }
	
}
